﻿using System;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using SessionHunter.Business.Exceptions;

namespace SessionHunter.Utilities
{
    /// <summary>
    /// Helper methods when dealing with ListViews
    /// </summary>
    public static class ListViewHelper
    {
        /// <summary>
        /// Adds the column if it does not already exist.
        /// Removes the column if it does exist
        /// </summary>
        /// <param name="view">The view to add to.</param>
        /// <param name="add">if set to <c>true</c> add the colummn, otherwise remove it.</param>
        /// <param name="header">The header field</param>
        /// <param name="binding">The name of the binding.</param>
        public static void DefineSingleColumn(GridView view, bool add, string header, string binding)
        {
            Contract.Requires(view != null);
            if (String.IsNullOrEmpty(header))
                throw new SessionApplicationException("Empty header passed to DefineSingleColumn");

            if (add)
            {
                // Adds the column if it does not exist
                foreach (GridViewColumn item in view.Columns)
                {
                    // The column is already there.  Don't add it again
                    if ((string)(item.Header) == header)
                        return;
                }

                // Not found, add it
                if (!String.IsNullOrEmpty(binding))
                    view.Columns.Add(new GridViewColumn() { DisplayMemberBinding = new Binding(binding), Header = header });
                else
                    view.Columns.Add(new GridViewColumn() { Header = header });
            }
            else
            {
                // Removes the column if it exists.
                foreach (var item in view.Columns)
                {
                    if ((string)(item.Header) == header)
                    {
                        // Found it, remove it
                        view.Columns.Remove(item);
                        return;
                    }
                }
                // Not found, do nothing
            }
        }

        /// <summary>
        /// Can be called from a ListViews SizeChanged event to automatically resize the columns.
        /// </summary>
        /// <param name="listView">The list view to resize.</param>
        /// <param name="columnWidths">The column widths for the ListView.</param>
        public static void SizeChangedHandler(ListView listView, params double[] columnWidths)
        {
            GridView gView = listView.View as GridView;
            if (gView.Columns.Count != columnWidths.Length)
                throw new UserInterfaceException("Number of columns passed to SizeChangedHandler for " + listView.Name + " do not match. Columns: " + gView.Columns.Count + " Configured: " + columnWidths.Length);

            var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth - 10; // take into account vertical scrollbar

            for (int i = 0; i < columnWidths.Length; i++)
            {
                gView.Columns[i].Width = workingWidth * columnWidths[i];
            }
        }
    }
}
